package com.lagou.service;

import com.lagou.common.entity.IdWorker;
import com.lagou.dao.RecruitDao;
import com.lagou.pojo.Recruit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class RecruitService {
    @Autowired
    private RecruitDao recruitDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询所有
     */
    public List<Recruit> findAll(){
        return recruitDao.findAll();
    }

    /**
     * 根据id查询
     */
    public Recruit findById(String id){
        return recruitDao.findById(id).get();
    }

    /**
     * 条件查询+分页
     */
    public Page<Recruit> findSearch(Map whereMap, int page, int size){
        Specification<Recruit> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page-1,size);
        return recruitDao.findAll(specification,pageRequest);
    }

    /**
     * 条件查询
     * @param whereMap
     * @return
     */
    public List<Recruit> findSearch(Map whereMap){
        Specification<Recruit> specification = createSpecification(whereMap);
        return recruitDao.findAll(specification);
    }

    /**
     * 增加
     */
    public void add(Recruit recruit){
        recruit.setId(idWorker.nextId()+"");
        recruitDao.save(recruit);
    }

    /**
     * 修改
     */
    public void update(Recruit recruit){
        recruitDao.save(recruit);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteById(String id){
        recruitDao.deleteById(id);
    }


    /**
     * 动态条件构建
     * @param searchMap
     * @return
     */
    private Specification<Recruit> createSpecification(Map searchMap) {
        return new Specification<Recruit>() {
            @Override
            public Predicate toPredicate(Root<Recruit> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<>();
                //id
                if (searchMap.get("id")!=null&&!"".equals(searchMap.get("id"))){
                    predicateList.add(cb.like(root.get("id").as(String.class),"%"+searchMap.get("id")+"%"));
                }
                // 企业ID
                if (searchMap.get("eid")!=null && !"".equals(searchMap.get("eid"))) {
                    predicateList.add(cb.like(root.get("eid").as(String.class), "%"+(String)searchMap.get("eid")+"%"));
                }
                // 职位名称
                if (searchMap.get("jobname")!=null && !"".equals(searchMap.get("jobname"))) {
                    predicateList.add(cb.like(root.get("jobname").as(String.class), "%"+(String)searchMap.get("jobname")+"%"));
                }
                // 薪资范围
                if (searchMap.get("salary")!=null && !"".equals(searchMap.get("salary"))) {
                    predicateList.add(cb.like(root.get("salary").as(String.class), "%"+(String)searchMap.get("salary")+"%"));
                }
                // 经验要求===>页面中死的下拉框
                if (searchMap.get("condition")!=null && !"".equals(searchMap.get("condition"))) {
                    predicateList.add(cb.like(root.get("condition").as(String.class), "%"+(String)searchMap.get("condition")+"%"));
                }
                // 学历要求
                if (searchMap.get("education")!=null && !"".equals(searchMap.get("education"))) {
                    predicateList.add(cb.like(root.get("education").as(String.class), "%"+(String)searchMap.get("education")+"%"));
                }
                // 任职方式 1全职  2兼职
                if (searchMap.get("type")!=null && !"".equals(searchMap.get("type"))) {
                    predicateList.add(cb.like(root.get("type").as(String.class), "%"+(String)searchMap.get("type")+"%"));
                }
                // 岗位标签
                if (searchMap.get("labels")!=null && !"".equals(searchMap.get("labels"))) {
                    predicateList.add(cb.like(root.get("labels").as(String.class), "%"+(String)searchMap.get("labels")+"%"));
                }
                // 状态 0审核中  1 上架  2下架
                if (searchMap.get("state")!=null && !"".equals(searchMap.get("state"))) {
                    predicateList.add(cb.like(root.get("state").as(String.class), "%"+(String)searchMap.get("state")+"%"));
                }
                // 职位诱惑
                if (searchMap.get("content1")!=null && !"".equals(searchMap.get("content1"))) {
                    predicateList.add(cb.like(root.get("content1").as(String.class), "%"+(String)searchMap.get("content1")+"%"));
                }
                // 职位要求
                if (searchMap.get("content2")!=null && !"".equals(searchMap.get("content2"))) {
                    predicateList.add(cb.like(root.get("content2").as(String.class), "%"+(String)searchMap.get("content2")+"%"));
                }
                // 工作地址
                if (searchMap.get("address")!=null && !"".equals(searchMap.get("address"))) {
                    predicateList.add(cb.like(root.get("address").as(String.class), "%"+(String)searchMap.get("address")+"%"));
                }
                // 是否热门 0不热门  1热门
                if (searchMap.get("ishot")!=null && !"".equals(searchMap.get("ishot"))) {
                    predicateList.add(cb.like(root.get("ishot").as(String.class), "%"+(String)searchMap.get("ishot")+"%"));
                }
                // 是否推荐 0 不推荐,1推荐
                if (searchMap.get("isrcommend")!=null && !"".equals(searchMap.get("isrcommend"))) {
                    predicateList.add(cb.like(root.get("isrcommend").as(String.class), "%"+(String)searchMap.get("isrcommend")+"%"));
                }
                // 公司评价  ID拼接字符串1,2,3
                if (searchMap.get("evaluate")!=null && !"".equals(searchMap.get("evaluate"))) {
                    predicateList.add(cb.like(root.get("evaluate").as(String.class), "%"+(String)searchMap.get("evaluate")+"%"));
                }
                // 公司备注
                if (searchMap.get("remark")!=null && !"".equals(searchMap.get("remark"))) {
                    predicateList.add(cb.like(root.get("remark").as(String.class), "%"+(String)searchMap.get("remark")+"%"));
                }
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * 查询出推荐岗位  默认为9个
     */
    public List<Recruit> findTop9ByIsrcommend(){
        return recruitDao.findTop9ByIsrcommend("1");
    }

    /**
     * 查询出热门岗位  默认为9个
     */
    public List<Recruit> findTop9ByIshot(){
        return recruitDao.findTop9ByIshot("1");
    }

    /**
     * 查询出最新岗位，按照日期的排序。默认为9个
     */
    public List<Recruit> findTop9ByStateOrderByCreatetimeDesc(){
        return recruitDao.findTop9ByStateOrderByCreatetimeDesc("1");
        //return recruitDao.findAll(Sort.by(Sort.Direction.DESC,"createtime"));
        //return recruitDao.findAll(new Sort(Sort.Direction.ASC,"createtime"));
    }
}
